<?php
namespace Common\Common\API\QZsoft;
use Common\Common\HttpClient;

/**
 * 强智教务处爬取类
 *
 * @version 0.3.0
 * @author raodaqi
 *         @time 2015.1.27 11:14
 */
class QZsoftJwc{
     /* 教务处登录 */
    public function login($cookies = '') {
        if ($cookies != null) {
            $this->cookies = $cookies;
            $myHttpClient = new HttpClient ();
            $myHttpClient->setCookies ($this->cookies);
            $url = $this->jwc_domain.'/JWXS/cjcx/jwxs_cjcx_like.aspx?usermain='.$this->account;
            $myHttpClient->get($url);
            $webContent = $myHttpClient->getContent ();
            if (strpos ( $webContent, iconv ( "UTF-8", "GBK", '开课学期' ) )) {
                return true;
            }
            $this->cookies = null;
            return false;
        }
        // 获取首页
        $jwc_cookie = $_SESSION ['jwc_cookie'];
        $myHttpClient = new HttpClient ();
        $myHttpClient->setCookies ( $jwc_cookie );
        // $myHttpClient->setReferer ( $this->jwc_domain. '/Login.aspx' );
        $myHttpClient->get ( $this->jwc_domain);
        $webTemp = $myHttpClient->getContent ();
        // 获得携带数据
         $postData = $this->loginMethod ( $webTemp );

        $myHttpClient->setCookies ( $jwc_cookie );
        // $myHttpClient->setReferer ( $this->jwc_domain . '/Login.aspx?zx=1' );
        $myHttpClient->post ( $this->jwc_domain . '/Login.aspx?zx=1 ', $postData );
        $this->cookies = $myHttpClient->getCookies();
        $webContent = $myHttpClient->getContent ();
        
        // 抛出错误异常
        if (strpos ( $webContent, iconv ( "UTF-8", "GBK", '用户名或密码错误' ) )) {
            throw new \Exception ( '用户名或密码不对，请重新输入', 2 );
        }
        if (strpos ( $webContent, iconv ( "UTF-8", "GBK", '验证码不正确，请重新输入!' ) )) {
            throw new \Exception ( '验证码错误，请重新输入' );
        }
        if (strpos ( $webContent, iconv ( "UTF-8", "GBK", '请不要在同一个IE下登陆两个帐号') )) {
            throw new \Exception ( '请不要在同一个IE下登陆两个帐号');
        }
        if (strpos ( $webContent, iconv ( "UTF-8", "GBK", '页面错误' ) )) {
            throw new \Exception ( '登录错误，请重新绑定');
        }
        
        // $this->cookies = $jwc_cookie;
         // $this->cookies = $myHttpClient->getCookies();
        if(empty($this->cookies)){
            throw new \Exception ( '啊偶~教务处君忙碌中，请耐心等待');
        }
        // print_r($webContent) ;
         return true;
    }
    
    /* 查询成绩 */
    public function queryGrade() {
        $myHttpClient = new HttpClient ();
       
        $myHttpClient->setCookies ( $this->cookies );
        
        $url = $this->jwc_domain.'/JWXS/cjcx/jwxs_cjcx_like.aspx?usermain='.$this->account;
        // print_r($url);die();
        $myHttpClient->get($url);
        $webContent = $myHttpClient->getContent ();

        $webContent = iconv ( "GBK", "UTF-8", $webContent );

        //$gpa = $this->parsingGpa($webContent);
        
        $pattern = "/<table id=\"cjxx\"[\s\S]*?<\/table>/";
        preg_match ( $pattern, $webContent, $table );

        $result = table_to_array ( $table [0] );

        $num = count ( $result );
       
        $grade = array ();
        // print_r($grade);die();
        for($j = 1; $j < $num; $j ++) {
            $year = substr ( $result [$j] [1], 0, 9 );
            $term = substr ( $result [$j] [1], 10, 11 );

            $grade [$j-1] ['year'] = $year;
            $grade [$j-1] ['term'] = $term;
            $grade [$j-1] ['course_code'] = $result [$j] [2];
            $grade [$j-1] ['course_name'] = $result [$j] [3];
            $grade [$j-1] ['grade'] = $result [$j] [4];
            $grade [$j-1] ['credit'] = $result [$j] [5];
            $grade [$j-1] ['period'] = $result [$j] [6];
            $grade [$j-1] ['course_nature'] = $result [$j] [7];
            $grade [$j-1] ['course_attribution'] = $result [$j] [8];

            $grade [$j-1] ['exam_type'] = $result [$j] [9];
            $grade [$j-1] ['rebuild_tag'] = $result [$j] [11];
        }
        // $grade['total_gpa'] = $gpa;
        $this->grades = $grade;
  
        return $this->grades;
    }
    
    public function getVerifyCode() {
        header ( 'Content-Type:image/bmp' );
        $myHttpClient = new HttpClient ();
        $myHttpClient->get ( $this->jwc_domain . '/Login.aspx?zx=1');
        $jwc_cookie = $myHttpClient->getCookies ();
        
        $_SESSION ['jwc_cookie'] = $jwc_cookie;
        
        // 得到验证码链接
        $url = $this->jwc_domain . '/other/CheckCode.aspx?datetime=az';
        // $myHttpClient->setReferer ( $this->jwc_domain . '/Login.aspx' );
        $myHttpClient->setCookies ( $jwc_cookie );
        $myHttpClient->get ( $url );
        $webContent = $myHttpClient->getContent ();
        echo $webContent;
    }
    

    // private function loginMethod($webTemp) {
    //         // 得到参数变量
    //         preg_match ( '/="__VIEWSTATE".*"/', $webTemp, $paraVie );

    //         $paraVie = urlencode ( substr ( $paraVie [0], 39, strlen ( $paraVie [0] ) - 40 ) ); 

    //         preg_match ( '/="__EVENTVALIDATION".*"/', $webTemp, $paraEve );
           
    //         $paraEve = urlencode ( substr ( $paraEve [0], 51, strlen ( $paraEve [0] ) - 52 ) );
    //         // 携带参数
    //         $postData = "__VIEWSTATE=$paraVie&Account=$this->account&PWD=$this->password&CheckCode=$this->verifycode&cmdok=&__EVENTVALIDATION=$paraEve";
    //         return $postData;
    // }
}
